package course.dao.impl;

import course.dao.BaseDao;
import course.dao.TeacherDao;
import course.pojo.entity.Teacher;
import course.pojo.vo.Page;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author HLF
 * @date 2022/6/30 20:56
 */
public class TeacherDaoImpl extends BaseDao<Teacher> implements TeacherDao {
    @Override
    public Teacher getById(Integer tid, Connection connection) {
        String sql = "select * from t_teacher where tid = ?;";
        return getOneBean(connection, sql, tid);
    }

    @Override
    public int addTeacher(Teacher teacher, Connection connection) {
        String sql = "insert into t_teacher(tid,tname,tsex,tphone,temail,cid,tpassword) values(?,?,?,?,?,?,?);";
        return update(connection, sql, teacher.getTid(), teacher.getTname(), teacher.getTsex(), teacher.getTphone(), teacher.getTemail(), teacher.getCid(), teacher.getTpassword());
    }

    @Override
    public int updateById(Teacher teacher, Connection connection) {
        String sql = "update t_teacher set tname = ?,tsex = ?,tphone = ?,temail = ?,cid=? where tid = ?;";
        return update(connection, sql, teacher.getTname(), teacher.getTsex(), teacher.getTphone(), teacher.getTemail(), teacher.getCid(), teacher.getTid());
    }

    @Override
    public int updateInfo(Teacher teacher, Connection connection) {
        String sql = "update t_teacher set tname = ?,tsex = ?,tphone = ?,temail = ?,tpassword = ? where tid = ?;";
        return update(connection, sql, teacher.getTname(), teacher.getTsex(), teacher.getTphone(), teacher.getTemail(), teacher.getTpassword(), teacher.getTid());
    }

    @Override
    public int deleteById(Integer tid, Connection connection) {
        String sql = "delete from t_teacher where tid = ?;";
        return update(connection, sql, tid);
    }

    @Override
    public List<Teacher> getList(Connection connection) {
        String sql = "select * from t_teacher;";
        return getBeanList(connection, sql);
    }

    /**/

    @Override
    public int deleteSelect(List<Integer> ids, Connection connection) {
        String sql = "delete from t_teacher where tid in(?)";

        String sqlIN = ids.stream()
                .map(x -> String.valueOf(x))
                .collect(Collectors.joining(",", "(", ")"));

        sql = sql.replace("(?)", sqlIN);

        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);

            return preparedStatement.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public long count(Connection connection) {
        String sql = "select count(*) from t_teacher;";
        return (long) getCount(connection, sql);
    }

    /**
     * 分页
     *
     * @param pageNo 当前页码数
     * @return 返回通用的分页对象
     */
    @Override
    public Page<Teacher> page(Integer pageNo, Connection connection) {
        Page<Teacher> page = new Page<>();
        Integer pageSize = page.getPageSize();

        String sql = "select * from t_teacher limit ?,?;";
        List<Teacher> list = getBeanList(connection, sql, (pageNo - 1) * pageSize, pageSize);

        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setList(list);
        long total = this.count(connection);
        page.setTotal(total);

        int pages = (int) Math.ceil((double) total / pageSize);
        page.setPages(pages);

        return page;
    }


    /**
     * 按照教师名称关键字查找，并返回分页对象
     */
    @Override
    public Page<Teacher> query(String tname, Integer pageNo, Connection connection) {
        Page<Teacher> page = new Page<>();
        Integer pageSize = page.getPageSize();

        String sql = "select * from t_teacher where tname like ? limit ?,?";
        String key = "%" + tname + "%";
        List<Teacher> list = getBeanList(connection, sql, key, (pageNo - 1) * pageSize, pageSize);

        String sql2 = "select count(*) from t_teacher where tname like ?;";
        long count = (long) getCount(connection,sql2,key);

        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setList(list);
        page.setTotal(count);

        int pages = (int) Math.ceil((double) count / pageSize);
        page.setPages(pages);

        return page;
    }

    @Override
    public List<Teacher> selectBathByIds(List<Integer> ids, Connection connection) {
        String sql = "select * from t_teacher where tid in(?)";

        String sqlIN = ids.stream()
                .map(x -> String.valueOf(x))
                .collect(Collectors.joining(",", "(", ")"));

        sql = sql.replace("(?)", sqlIN);

        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            ResultSet rs = preparedStatement.executeQuery();
            List<Teacher> teachers = new ArrayList<>();
            while (rs.next()) {
                Teacher teacher = new Teacher();
                teacher.setTid(rs.getInt(1));
                teacher.setTname(rs.getString(2));
                teacher.setTsex(rs.getString(3));
                teacher.setTphone(rs.getString(4));
                teacher.setTemail(rs.getString(5));
                teacher.setCid(rs.getInt(6));
                teacher.setTpassword(rs.getString(7));

                teachers.add(teacher);
            }
            return teachers;

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
